#include  <iostream>
#include  <iomanip>
using  namespace  std;

class Vector
{
public:
    Vector(int n=0):len(n)
    {
        if(n>0)
        {
            parr=new int[len];
            for(int i=0; i<len; i++)
                parr[i]=0;
        }
        else
            parr= NULL;
        cout<<"1 para constructor,this is "<<this<<endl;
    }

    Vector(int n,int arr[]):len(n)
    {
        if(n>0)
        {
            parr=new int[len];
            for(int i=0; i<len; i++)
                parr[i]=arr[i];
        }
        else
            parr= NULL;
        cout<<"2 para constructor,this is "<<this<<endl;
    }

    //拷贝构造，深拷贝
    Vector(const Vector& other):len(other.len)
    {
        parr=new int[len];
        for(int i=0; i<len; i++)
            parr[i]=other.parr[i];
        cout<<"copy constructor,this is "<<this<<" ,other is "<< &other<<endl;
    }

    //析构
    ~Vector()
    {
        cout<<"Destructor,this is "<<this<<endl;
        if(parr!= NULL)
            delete []parr;
    }

    void print()
    {
        cout<<"Vector length: "<<len<<"\nData are:   ";
        for(int i=0; i<len; i++)
            cout<<setw(5)<<parr[i];
        cout<<endl<<endl;
    }

    void setElem(int iter,int val)
    {
        if(iter>=0 && iter<len)
            parr[iter]=val;
    }

    void pushBack(int val)//追加一个元素
    {
        if(parr!=NULL)
        {
            int *ptmp=new int[len+1];
            for(int i=0; i<len; i++)
                ptmp[i]=parr[i];
            ptmp[len]=val;
            len++;

            delete []parr;
            parr=ptmp;
        }
        else
        {
            parr=new int[1];
            parr[0]=val;
            len=1;
        }
    }

    //赋值重载，深拷贝
    Vector& operator= (const Vector& other )
    {
        cout<<"= operator,this is "<<this<<" ,other is "<< &other<<endl;
        if( this== &other)
            return *this;
        else
        {
            delete[] parr;
            len=other.len;
            parr=new int[len];
            for(int i=0; i<len; i++)
                parr[i]=other.parr[i];
            return *this;
        }
    }

    //移动赋值重载，浅拷贝
    Vector& operator= (Vector&& other )
    {
        cout<<"move = operator,this is "<<this<<" ,other is "<< &other<<endl;
        len=other.len;
        parr=other.parr;
        other.len=0;
        other.parr=NULL;
        return *this;
    }

    //移动构造，浅拷贝
    Vector( Vector&& other):len(other.len),parr(other.parr)
    {
        other.len=0;
        other.parr=NULL;
        cout<<"move constructor,this is "<<this<<" ,other is "<< &other<<endl;
    }

private:
    int* parr;
    int len;
};

Vector fun()
{
    int a[6]= {8,6,7,5,3,9};
    Vector obj(6,a);
    return obj;
}

int  main()
{
    Vector objD(4);
    cout<<"******** objD before ***********\n";
    objD.print();
    objD=fun();
    cout<<"******** objD after ***********\n";
    objD.print();

    cout<<"******** END ***********\n";
    return  0;
}